import sys
#from PyQt5.QtCore import SIGNAL
#from PyQt4.QtGui import *
from PyQt5.QtGui import QFont,QFontMetrics,QColor
from PyQt5.Qsci import QsciScintilla, QsciLexerCMake
from PyQt5.QtWidgets import *

class SimplePythonEditor(QsciScintilla):
    ARROW_MARKER_NUM = 8
    BREAK_MARKER_NUM = 9

    def __init__(self, parent=None):
        super(SimplePythonEditor, self).__init__(parent)
        self.runLine = 1

        # Set the default font
        font = QFont()
        font.setFamily('Courier')
        font.setFixedPitch(True)
        font.setPointSize(10)
        self.setFont(font)
        self.setMarginsFont(font)

        # Margin 0 is used for line numbers
        fontmetrics = QFontMetrics(font)
        self.setMarginsFont(font)
        self.setMarginWidth(0, fontmetrics.width("00000") + 6)
        self.setMarginLineNumbers(0, True)
        self.setMarginsBackgroundColor(QColor("#cccccc"))

        # Clickable margin 1 for showing markers
        self.setMarginSensitivity(1, True)
        self.marginClicked.connect(self.on_margin_clicked)
        #self.connect(self,
        #    SIGNAL('marginClicked(int, int, Qt::KeyboardModifiers)'),
        #    self.on_margin_clicked)
        self.markerDefine(QsciScintilla.RightArrow,
            self.ARROW_MARKER_NUM)
        self.markerDefine(QsciScintilla.Circle,
            self.BREAK_MARKER_NUM)
        self.setMarkerBackgroundColor(QColor("#32CD32"),
            self.ARROW_MARKER_NUM)
        self.setMarkerBackgroundColor(QColor("#ee1111"),
            self.BREAK_MARKER_NUM)

        # Brace matching: enable for a brace immediately before or after
        # the current position
        #
        #self.setBraceMatching(QsciScintilla.SloppyBraceMatch)

        # Current line visible with special background color
        self.setCaretLineVisible(True)
        self.setCaretLineBackgroundColor(QColor("#ffe4e4"))

        # Set Python lexer
        # Set style for Python comments (style number 1) to a fixed-width
        # courier.
        #
        #lexer = QsciLexerPython()
        lexer = QsciLexerCMake()
        lexer.setDefaultFont(font)
        self.setLexer(lexer)
        #self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, 'Courier')

        # Don't want to see the horizontal scrollbar at all
        # Use raw message to Scintilla here (all messages are documented
        # here: http://www.scintilla.org/ScintillaDoc.html)
        self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)

        # not too small
        #self.setMinimumSize(600, 450)

        #custom properties
        self.breakPoints = []

    def on_margin_clicked(self, nmargin, nline, modifiers):
        # Toggle marker for the line the margin was clicked on
        if self.markersAtLine(nline) != 0:
            #self.markerDelete(nline, self.ARROW_MARKER_NUM)
            self.markerDelete(nline, self.BREAK_MARKER_NUM)
            self.breakPoints.remove(nline)
        else:
            #self.markerAdd(nline, self.ARROW_MARKER_NUM)
            self.markerAdd(nline, self.BREAK_MARKER_NUM)
            self.breakPoints.append(nline)
            self.breakPoints.sort()
            #print "add break %d. breakPoints = %s"%(nline,self.breakPoints)

    def move_cursor_to(self,line):
        #print "move to line %d.breaks = %s"%(line,self.breakPoints)
        self.markerDelete(self.runLine, self.ARROW_MARKER_NUM)
        self.markerAdd(line, self.ARROW_MARKER_NUM)
        #if line in self.breakPoints:
        #    self.markerDelete(line,self.BREAK_MARKER_NUM)
            #print "deleted line %d"%line
        self.runLine = line
        self.setCursorPosition(line,0);




if __name__ == "__main__":
    app = QApplication(sys.argv)
    editor = SimplePythonEditor()
    editor.show()
    editor.setText(open(sys.argv[0]).read())
    app.exec_()
